import BannerAdView from "./BannerAdView";
import CenterAdView from "./CenterAdView";
import FriendPlayView from "./FriendPlayView";
import MainAdViewUI from "./MainAdView";
import { BaseAdView } from "./BaseAdView";
import PopularrecommendView from "./PopularrecommendView";
import FightOverAdView from "./FightOverAdView";
import GameAdsManager, { PlatformType } from "../GameAdsManager";
import ThreeSmallAdView from "./ThreeSmallAdView";


//AdType 0抽屉 1底部Banner 2主界面2两个 3 好友都在玩 4热门推荐 5结算界面
export class AdConfigData {
    public AppId: string = "";
    public AdType: number = 0;
    public AppName: string = "";
    public Type: number = 2;   //小程序产品类型：1-卖量；2-自研；3-自研补位
    public AppIcon: string = "";
    public ShowHot: number = 1;
    public Path: string = "";  //微信小程序的跳转路径
    public Type0: number = 10001;//1 Type0 抽屉
    public Type1: number = 10001;//2 Type1 底部banner
    public Type2: number = 10001;//3 Type2 闯关导出位
    public Type3: number = 10001;//4 Type3 好友都在玩
    public Type4: number = 10001;//5 Type4 热门推荐
    public Type5: number = 10001;//6 Type5 结算界面
    public Type6: number = 10001;//7 Type6 首页顶部抽屉
    public Type7: number = 10001;//8 Type7 道具球奖励弹窗
    public Type8: number = 10001;//9 Type8 预留广告位2
    public Type9: number = 10001;//10 Type9 预留广告位3
    public Type10: number = 10001;//11 Type10 三个广告位
}

export enum MoveDir {
    Forward = "Forward",
    Back = "Back",
    Up = "Up",
    Down = "Down",
    None = "None"
}

export default class ShiyouAdManager {

    private static m_instance: ShiyouAdManager;

    public static get Instance() {
        if (this.m_instance == null) {
            this.m_instance = new ShiyouAdManager();
        }
        return this.m_instance;
    }

    private m_adviewmap = {};

    private m_adConfigTestPath = "http://192.168.1.200:8099/product_config/config_list?app_id=wx39778cbf9f4090a1";
    private m_adConfigEnvPath = "https://wxex.game520.com/prod-api/product_config/config_list?app_id=wx39778cbf9f4090a1";
    private m_adconfigs: Array<AdConfigData> = [];
    private m_timecount:number=0;

    GetAdConfigDatas() {
        return this.m_adconfigs;
    }

    Init() {
        this.InitMap();
        this.LoadAdConfig();
    }


    private InitMap()  {
        this.m_adviewmap[BannerAdView.name] = BannerAdView;
        this.m_adviewmap[CenterAdView.name] = CenterAdView;
        this.m_adviewmap[PopularrecommendView.name] = PopularrecommendView;
        this.m_adviewmap[FriendPlayView.name] = FriendPlayView;
        this.m_adviewmap[MainAdViewUI.name] = MainAdViewUI;
        this.m_adviewmap[FightOverAdView.name] = FightOverAdView;
        this.m_adviewmap[ThreeSmallAdView.name]=ThreeSmallAdView;
    }

    private m_adview: { [keykey: string]: BaseAdView; } = {};

    private LoadAdConfig()  {
        switch (GameAdsManager.Instance.Platform) {
            case PlatformType.WeChat:
            case PlatformType.None:
                 this.m_timecount= Laya.Browser.now();
                var xhr = new Laya.HttpRequest();
                xhr.http.timeout = 10000;//设置超时时间；
                xhr.once(Laya.Event.COMPLETE, this, this.OnLoadComplete);
                xhr.once(Laya.Event.ERROR, this, errorHandler);
                xhr.on(Laya.Event.PROGRESS, this, processHandler);
                var headers = ["Content-Type", "application/json"];
                xhr.send(this.m_adConfigEnvPath, null, "get", "json", headers);

                function processHandler(data) {
                }
                function errorHandler(data) {
                    console.error("LoadAdConfig error " + data);
                }
                break;
            case PlatformType.QQ:

                break;

            default:
                break;
        }

    }


    private OnLoadComplete(res)  {
        if (GameAdsManager.Instance.Platform == PlatformType.None) {
        console.log(JSON.stringify(res));
        }
      console.warn("OnLoadComplete "+ new Date().toTimeString()) 
        this.m_adconfigs = res.data;
        for (var key in this.m_adview) {
            this.m_adview[key].InitData(this.m_adconfigs);
        }
    }


    ShowAdView(viewName, param?) {
        switch (GameAdsManager.Instance.Platform) {
            case PlatformType.None:
            case PlatformType.WeChat:
                if (this.m_adview[viewName] == null) {
                    let view = new this.m_adviewmap[viewName];
                    this.m_adview[viewName] = view;
                    this.m_adview[viewName].InitData(this.m_adconfigs);
                }
                this.m_adview[viewName].Show(param);
                break;

            default:
                break;
        }

    }


    HidAdView(viewName) {
        if (this.m_adview[viewName] == null) {
            console.error("not found innerView " + viewName);
            return;
        }
        this.m_adview[viewName].Hide();
    }


    public SetFriendViewHideCallBack(callback: Function)  {
        if (this.m_adview[FriendPlayView.name] != null) {
            (this.m_adview[FriendPlayView.name] as FriendPlayView).SetHideCallBack(callback);
        }
    }


    public CancelFriendViewHideCallBack(callback: Function) {
        if (this.m_adview[FriendPlayView.name] != null) {
            (this.m_adview[FriendPlayView.name] as FriendPlayView).CancelHideCallBack();
        }
    }

}